package gov.pbc.nn.kjgl.fxq.organProblem.view;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.exception.NoneLoginException;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.bdf.entity.Dept;
import gov.pbc.nn.core.dao.CommonHibernateDao;
import gov.pbc.nn.kjgl.fxq.attachment.AttachmentEntity;
import gov.pbc.nn.kjgl.fxq.organProblem.entity.OrganProblemEntity;
import gov.pbc.nn.kjgl.fxq.workDocument.view.WorkDocumentPR;

@Component(OrganProblemPR.BEAN_ID)
public class OrganProblemPR extends CommonHibernateDao {
	public static final String BEAN_ID = "organProblemPR";
	
	@Resource(name = WorkDocumentPR.BEAN_ID)
	private WorkDocumentPR workDocumentPR;

	@DataProvider
	public void loadOrganProblems(Page<OrganProblemEntity> page, Criteria criteria) throws Exception {
		IUser user = ContextHolder.getLoginUser();
		if (user == null)
			throw new NoneLoginException("请先登录！");
		if (user.getDepts() == null || user.getDepts().isEmpty())
			throw new NoneLoginException("您未设置部门！");
		Dept dept = (Dept) user.getDepts().get(0);
		List<String> childrenIds = this.workDocumentPR.getAllChildOrgDeptId(dept.getCDept().getParentId());
		// System.out.println(dept.getCDept().getName());
		childrenIds.add(dept.getCDept().getId());
		DetachedCriteria detachedCriteria = this.buildDetachedCriteria(criteria, OrganProblemEntity.class);
		detachedCriteria.add(Restrictions.or(Restrictions.eq("createUser", user.getUsername()), Restrictions.and(Restrictions.in("deptId", childrenIds), Restrictions.eq("state", 20))));
		detachedCriteria.add(Restrictions.eq("invalid", false));
		if (criteria != null && criteria.getOrders() != null && !criteria.getOrders().isEmpty()) {
			com.bstek.dorado.data.provider.Order order = criteria.getOrders().get(0);
			if (order.isDesc()) {
				detachedCriteria.addOrder(Order.desc(order.getProperty()));
			} else {
				detachedCriteria.addOrder(Order.asc(order.getProperty()));
			}
		} else {
			detachedCriteria.addOrder(Order.desc("createDate"));
		}
		this.pagingQuery(page, detachedCriteria);
	}

	@DataResolver
	@Transactional
	public void saveOrganProblems(List<OrganProblemEntity> organProblems) throws Exception {
		IUser user = ContextHolder.getLoginUser();
		if (user == null)
			throw new NoneLoginException("请先登录！");
		if (user.getDepts() == null || user.getDepts().isEmpty())
			throw new NoneLoginException("您未设置部门！");
		Dept dept = (Dept) user.getDepts().get(0);
		for (OrganProblemEntity organProblem : organProblems) {
			EntityState state = EntityUtils.getState(organProblem);
			OrganProblemEntity organProblemEntity = EntityUtils.toEntity(organProblem);
			organProblem.setState(10);
			if (state.equals(EntityState.NEW)) {
				organProblem.setDeptId(dept.getCDept().getId());
				organProblem.setDeptName(dept.getCDept().getName());
				this.save(organProblem);
			} else if (state.equals(EntityState.MODIFIED)) {
				this.update(organProblem);
			} else if (state.equals(EntityState.DELETED)) {
				this.delete(organProblem);
			}

			List<AttachmentEntity> attachments = EntityUtils.getValue(organProblemEntity, "attachments");
			if (attachments != null) {
				this.update(organProblem);
				for (AttachmentEntity attachmentEntity : attachments) {
					this.workDocumentPR.saveEntity(attachmentEntity);
				}
			}
		}

	}

	@Expose
	@DataResolver
	@Transactional
	public void submitOrganProblem(OrganProblemEntity organProblem) throws Exception {
		if (organProblem.getState() != 20) {
			organProblem.setState(20);
			organProblem.setSubmitDate(new Date());
			this.update(organProblem);
		}
	}
}
